package org.ainlolcat.ainscience.statistics.handlers.hitran.tests;

import org.ainlolcat.ainscience.statistics.handlers.hitran.SpectrGenerator;
import org.ainlolcat.ainscience.statistics.handlers.hitran.instfunc.GaussInstrumentalFunction;
import org.ainlolcat.ainscience.statistics.handlers.hitran.instfunc.InstrumentalFunction;
import org.ainlolcat.ainscience.statistics.handlers.hitran.instfunc.InvsPolyFunction;
import org.ainlolcat.ainscience.statistics.handlers.hitran.instfunc.PsdVoigt1InstrumentalFunction;
import org.ainlolcat.ainscience.utils.ArraysUtil;
import org.math.plot.Plot2DPanel;
import org.math.plot.Plot3DPanel;

import javax.swing.*;
import java.io.IOException;

/**
 * @author ainlolcat
 * Date: 13/11/20
 */
public class ViewNormintegralDependencyFromWidthAndT {

    public static void main(String[] args) throws IOException {
        //plotIntegrals();
        //plot_aXSpectrSample();
        //plot_bXSpectrSample();
        plotaXIntegrals4FixedWidth(20, 280, 350);
        plotaXIntegrals4FixedWidth(100, 80, 500);
        plotbXIntegrals4FixedWidth();
        //plotSpectrSample4InvsPoly();
    }

    public static void plotIntegrals() throws IOException {
        int wSize = 20;
        double wStart = 0.5;
        double wEnd = 1.5;
        int tSize = 20;
        double tStart = 80;
        double tEnd = 500;
        double[][] data = new double[wSize*tSize][3];

        SpectrGenerator sg = new SpectrGenerator();


        for (int i=0;i<wSize;i++){
            double w = wStart + i*(wEnd - wStart)/wSize;
            for(int j=0;j<tSize;j++){
                double T = tStart + j*(tEnd - tStart)/tSize;;
                data[i*wSize + j][0] = w;
                data[i*wSize + j][1] = T;

                double[][] spectr = sg.getAxSpectr(T, new GaussInstrumentalFunction(w), 1240, 1300, 0.25);
                double[] x = new double[spectr.length];
                double[] y = new double[spectr.length];
                for (int k=0;k<x.length;k++){
                    x[k]=spectr[k][0];
                    y[k]=spectr[k][1];
                }
                data[i*wSize + j][2] = ArraysUtil.getNormalizedIntegral(x, y);
            }
        }
        Plot3DPanel plot = new Plot3DPanel();
        plot.addScatterPlot("",data);
        JFrame frame = new JFrame("Dependency of a-X integral from T and width");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setContentPane(plot);
        frame.setVisible(true);
    }

    public static void plotaXIntegrals4FixedWidth(int tSize, double tStart, double tEnd) throws IOException {
        double[] x = new double[tSize];
        double[] y = new double[tSize];

        SpectrGenerator sg = new SpectrGenerator();

        for(int j=0;j<tSize;j++){
            double T = tStart + j*(tEnd - tStart)/tSize;;
            x[j] = T;

            double[][] spectr = sg.getAxSpectr(T, new InvsPolyFunction(0, 1.9653, 1E9, 0, 0, 1.3605), 1240, 1300, 0.25);
            double[] xS = new double[spectr.length];
            double[] yS = new double[spectr.length];
            for (int k=0;k<xS.length;k++){
                xS[k]=spectr[k][0];
                yS[k]=spectr[k][1];
            }
            y[j] = ArraysUtil.getNormalizedIntegral(xS, yS);
        }

        Plot2DPanel plot = new Plot2DPanel();
        plot.addLinePlot("",x,y);
        JFrame frame = new JFrame("a-X norm integral from T");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setContentPane(plot);
        frame.setVisible(true);
    }

    static double psd_A=1, psd_w=0.60814,  psd_y0 =0,  mu = 0.07335; //ava2048-1 bc600mk 772nm
    static InstrumentalFunction function = new PsdVoigt1InstrumentalFunction(psd_y0, psd_A, psd_w, mu);
    public static void plotbXIntegrals4FixedWidth() throws IOException {
        int tSize = 20;
        double tStart = 80;
        double tEnd = 500;
        double[] x = new double[tSize];
        double[] y = new double[tSize];

        SpectrGenerator sg = new SpectrGenerator();

        for(int j=0;j<tSize;j++){
            double T = tStart + j*(tEnd - tStart)/tSize;;
            x[j] = T;

            double[][] spectr = sg.getBxSpectr(T, function, 755, 775, 0.25);
            double[] xS = new double[spectr.length];
            double[] yS = new double[spectr.length];
            for (int k=0;k<xS.length;k++){
                xS[k]=spectr[k][0];
                yS[k]=spectr[k][1];
            }
            y[j] = ArraysUtil.getNormalizedIntegral(xS, yS);
        }

        Plot2DPanel plot = new Plot2DPanel();
        plot.addLinePlot("",x,y);
        JFrame frame = new JFrame("b-X norm integral from T");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setContentPane(plot);
        frame.setVisible(true);
    }

    public static void plot_bXSpectrSample() throws IOException {
        SpectrGenerator sg = new SpectrGenerator();
        double[][] spectr = sg.getBxSpectr(200, function, 755, 775, 0.25);
        double[] x = new double[spectr.length];
        double[] y = new double[spectr.length];
        for (int k=0;k<x.length;k++){
            x[k]=spectr[k][0];
            y[k]=spectr[k][1];
        }
        Plot2DPanel plot = new Plot2DPanel();
        plot.addLinePlot("",x,y);
        JFrame frame = new JFrame("b-X sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setContentPane(plot);
        frame.setVisible(true);
    }

    public static void plot_aXSpectrSample() throws IOException {
        SpectrGenerator sg = new SpectrGenerator();
        double[][] spectr = sg.getAxSpectr(200, new GaussInstrumentalFunction(1), 1240, 1300, 0.25);
        double[] x = new double[spectr.length];
        double[] y = new double[spectr.length];
        for (int k=0;k<x.length;k++){
            x[k]=spectr[k][0];
            y[k]=spectr[k][1];
        }
        Plot2DPanel plot = new Plot2DPanel();
        plot.addLinePlot("",x,y);
        JFrame frame = new JFrame("Gauss");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setContentPane(plot);
        frame.setVisible(true);
    }

    public static void plotSpectrSample4InvsPoly() throws IOException {
        SpectrGenerator sg = new SpectrGenerator();
        double[][] spectr = sg.getAxSpectr(200, new InvsPolyFunction(0, 1.9653, 1E9, 0, 0, 1.3605), 1240, 1300, 0.25);
        double[] x = new double[spectr.length];
        double[] y = new double[spectr.length];
        for (int k=0;k<x.length;k++){
            x[k]=spectr[k][0];
            y[k]=spectr[k][1];
        }
        Plot2DPanel plot = new Plot2DPanel();
        plot.addLinePlot("",x,y);
        JFrame frame = new JFrame("InvsPoly");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setContentPane(plot);
        frame.setVisible(true);
    }
}
